package com.zxl.grocery.modular.crawler.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * List工具类
 * Created by zxl on 2017/8/11 0011.
 */
public class ListUtils {
    /**
     * 比较两个List<String>的不同元素，并返回不同的信息
     *
     * @param list1 大的
     * @param list2 小的
     * @return
     */
    public static List<String> getDiffrentLists(List<String> list1, List<String> list2) {
        // long st = System.nanoTime();
        List<String> diff = new ArrayList<String>();
        List<String> maxList = list1;
        List<String> minList = list2;
        if (list2.size() > list1.size()) {
            maxList = list2;
            minList = list1;
        }
        Map<String, Integer> map = new HashMap<String, Integer>(maxList.size());
        for (String string : maxList) {
            map.put(string, 1);
        }
        for (String string : minList) {
            if (map.get(string) != null) {
                map.put(string, 2);
                continue;
            }
            diff.add(string);
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                diff.add(entry.getKey());
            }
        }
        // System.out.println("getDiffrentLists total times "+(System.nanoTime()-st));
        return diff;

    }
}
